﻿@model DotNet.Highcharts.Highcharts
@{
    ViewBag.Title = "Ajax loaded data, clickable points";
}
@(Model)

<script type="text/javascript">
    var filePath = '@Url.Content("~/Content/analytics.tsv")';
$(function(){
    // Load data asynchronously using jQuery. On success, add the data
   // to the options and initiate the chart.
   // This data is obtained by exporting a GA custom report to TSV.
   // http://api.jquery.com/jQuery.get/
    jQuery.get(filePath, null, function (tsv, state, xhr) {
      var lines = [],
         listen = false,
         date,
         
         // set up the two data series
         allVisits = [],
         newVisitors = [];
         
      // inconsistency
      if (typeof tsv !== 'string') {
         tsv = xhr.responseText;
      }
      
      // split the data return into lines and parse them
      tsv = tsv.split(/\n/g);
      jQuery.each(tsv, function(i, line) {

         // listen for data lines between the Graph and Table headers
         if (tsv[i - 3] == '# Graph') {
            listen = true;
         } else if (line == '' || line.charAt(0) == '#') {
            listen = false;
         }
         
         // all data lines start with a double quote
         if (listen) {
            line = line.split(/\t/);
            date = Date.parse(line[0] +' UTC');
            
            allVisits.push([
               date, 
               parseInt(line[1].replace(',', ''), 10)
            ]);
            newVisitors.push([
               date, 
               parseInt(line[2].replace(',', ''), 10)
            ]);
         }
      });
      
      chart.series[0].setData(allVisits);
      chart.series[1].setData(newVisitors);
      
       
   });
})
</script>